package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;



/**
 * Servlet implementation class servlet.postings
 */
@WebServlet("/servlet.postings")
public class postings extends HttpServlet {
	private static final long serialVersionUID = 1L;
	sqliteDo slqlt;
	List<String> list;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public postings() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		slqlt = new sqliteDo();
        list = slqlt.selectTalk();
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		String data = request.getParameter("data");
		//int a = Integer.parseInt(data);
		System.out.println(data);
		//System.out.println(list.size());
		
		
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");// ����Ϊutf-8��ֹǰ���յ�������Ϊ����
		PrintWriter out = response.getWriter();
 
		Gson gson = new Gson();
		ArrayList<postingsjosn> arraylistTest = new ArrayList<postingsjosn>();
 
		for(int i = 0; i < list.size(); i+=4)
		{
			postingsjosn t1 = new postingsjosn();
			t1.settitle(list.get(i));
			t1.settext(list.get(i+1));
			t1.setauthor(list.get(i+2));
			t1.setdate(list.get(i+3));
			arraylistTest.add(t1);
		}
			
		String info = gson.toJson(arraylistTest);
		// ��ӡ���z��
		System.out.println("��ˣ�"+info);
		// ���ؽoǰ��
		out.write(info);
	
		}
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
class postingsjosn{
	private String title;
	private String text;
	private String author;
	private String date;
	
	public String gettitle() {
		return title;
	}
	public void settitle(String data) {
		this.title = data;
	}
	public String gettext() {
		return text;
	}
	public void settext(String data) {
		this.text = data;
	}
	public String getauthor() {
		return author;
	}
	public void setauthor(String data) {
		this.author = data;
	}
	public String getdate() {
		return date;
	}
	public void setdate(String data) {
		this.date = data;
	}
}